package com.zbcbms.controller;

import com.alibaba.druid.util.StringUtils;
import com.zbcbms.Main;
import com.zbcbms.common.AlertUtilCommon;
import com.zbcbms.entity.User;
import com.zbcbms.entity.dto.UserMap;
import com.zbcbms.service.UserService;
import com.zbcbms.service.impl.UserServiceImpl;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ResourceBundle;

/**
 * @author fengq
 */
public class UpdatePersonController implements Initializable {

    @FXML
    private TextField updateUsername;
    @FXML
    private TextField updateUserNumber;
    @FXML
    private TextField updateJoinTime;
    @FXML
    private TextField updateOldPassword;
    @FXML
    private TextField updateNewPassword;

    private AlertUtilCommon alertUtilCommon=new AlertUtilCommon();

    private UserService userService=new UserServiceImpl();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        userMessageInit();
    }

    @FXML
    public void resetUserAction(){
        updateUsername.setText("");
        updateOldPassword.setText("");
        updateNewPassword.setText("");
    }

    public void userMessageInit(){
        updateUserNumber.setText(UserMap.getNumber());
        updateUsername.setText(UserMap.getName());
        updateJoinTime.setText(UserMap.getJoinTime().toString());
    }

    public void updateUserAction(){
        String username=updateUsername.getText();
        String userNumber=updateUserNumber.getText();
        String joinTime = updateJoinTime.getText();
        String oldPassword=updateOldPassword.getText();
        String newPassword=updateNewPassword.getText();
        if (StringUtils.isEmpty(username)||StringUtils.isEmpty(oldPassword)||StringUtils.isEmpty(newPassword)){
            alertUtilCommon.AlterMessageInform(updateUsername.getScene().getWindow(), Alert.AlertType.ERROR,"错误提醒","参数不能为空","参数为空警告");
        }else {
            String passwordByNumber = userService.getPasswordByNumber(userNumber);
            if (!passwordByNumber.equals(oldPassword)){
                alertUtilCommon.AlterMessageInform(updateNewPassword.getScene().getWindow(), Alert.AlertType.ERROR,"修改提醒","旧密码不对","小仲书：差亿点点成功");
            }else {
                if (newPassword.length()<6){
                    alertUtilCommon.AlterMessageInform(updateNewPassword.getScene().getWindow(), Alert.AlertType.ERROR,"修改提醒","新密码不够6位","小仲书：差亿点点成功");
                }else {
                    try {
                        Date date = new SimpleDateFormat("yyyy-MM-dd").parse(joinTime);
                        User user = new User();
                        user.setUserName(username);
                        user.setPassword(newPassword);
                        user.setUserNumber(userNumber);
                        user.setJoinDate(date);
                        user.setRole(UserMap.getRole());
                        Boolean aBoolean = new UserServiceImpl().updateById(user);
                        if (aBoolean){
                            alertUtilCommon.AlterMessageInform(updateUsername.getScene().getWindow(),Alert.AlertType.CONFIRMATION,"修改提示","修改成功","成功提醒");
                            alertUtilCommon.AlterMessageInform(updateUsername.getScene().getWindow(),Alert.AlertType.CONFIRMATION,"错误提示","登陆账号密码不对","小仲书：用新密码登陆就可以");
                            Stage stage=(Stage) updateUsername.getScene().getWindow();
                            stage.close();
                            UserMap.setName(null);
                            UserMap.setNumber(null);
                            UserMap.setPassword(null);
                            UserMap.setRole(null);
                            new Main().start(stage);
                        }else {
                            alertUtilCommon.AlterMessageInform(updateUsername.getScene().getWindow(),Alert.AlertType.ERROR,"错误提醒","修改失败","操作失败");
                        }
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }

            }

        }
    }
}
